﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using System;
using System.IO;
using System.Text.Json.Serialization;
using Xc.WaterMeters.ClientHost.Services;

namespace Xc.WaterMeters.ClientHost
{
    public class Startup
    {
        private readonly IConfiguration configuration;

        public Startup(IConfiguration configuration)
        {
            this.configuration = configuration;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers().AddJsonOptions(s =>
            {
                s.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
            });
            services.AddWaterMeterDevice<DeviceCallBackService>();
            services.AddSwaggerGen(c =>
            {
                var files = Directory.GetFiles(Path.GetDirectoryName(this.GetType().Assembly.Location), "*.xml");
                foreach (var item in files)
                {
                    c.IncludeXmlComments(item);
                }
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "水表调用接口说明", Version = "v1" });
            });
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseDeveloperExceptionPage();
            app.UseRouting();
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.RoutePrefix = String.Empty;
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "水表调用接口说明 v1");
            });
            app.UseEndpoints(s =>
            {
                s.MapControllers();
            });

            app.UseMeterDevice("http://localhost:5000");
        }
    }
}
